﻿Rsd.define('Rsd.Faith.pages.ListPage', {
    extend: 'Rsd.view.BaseListPage',
    requires: [
        'Rsd.Faith.stores.ListStore',
        'Rsd.controlEx.ModelViewer'
    ],
    xtype: 'list-page',
    border: false,
    tabTitle: '数据列表',
    layout: 'fit',
    dataStore:'listStore',
    selected:true,
    readOnly:false,
    templateCols: [
        {
            name: 'edit',
            text: '操 作',
            nobr:true,
            xtype: 'template',
            format:'templateFormat'
        },
        {
            name: 'space',
            text: '',
            xtype: 'template',
            width: 5,
            hideable: false,
            sortable: false
        }
    ],
    /*
     * [{name:'',text:'',dataIndex:'',xtype:'',op:''},{name:'',text:'',dataIndex:'',xtype:'',op:''}]
     * */
    //searchColumns:[],
    /*
     * */
    constructor: function ListPage(config) {
        config = config || {};
        this.apply(config);

        if(Rsd.isString(this.dataStore))
        {
            this.dataStore = Rsd.widget(this.dataStore);
        }
    },
    /*
     * */
    onBeforeInit:function onBeforeInit()
    {
        this.callParent();
        var me = this;

        if(Rsd.isEmpty(this.modelType) )
        {
            throw new Error('Property modelType is null.');
        }

        this.getSchema();
    },
    /*
     *
     * */
    templateFormat:function templateFormat(row) {

        if(this.readOnly)
        {
            return [];
        }
        var me = this;
        var  btn = document.createElement('a');
        btn.href='#';
        btn.innerHTML = '修改';
        $(btn).click (function(){
            me.editItem(row);
        } );

        var  btnDel = document.createElement('a');
        btnDel.href='#';
        btnDel.innerHTML = '删除';
        $(btnDel).click (function(){
            me.deleteItem(row);
        } );

        return [btn,$('<span>&nbsp;&nbsp;</span>')[0],btnDel] ;
    },
    /*
     *
     * */
    getSchema:function getSchema()
    {
        var me = this;

        if(Rsd.isEmpty( me.formFields) || Rsd.isEmpty( me.gridColumns))
        {
            var _type = this.modelType;
            if(!_type.startWith('Rsd.'))
            {
                _type = Rsd.app.assembelyName + '.' + this.modelType;
            }

            Rsd.app.getSchema(_type,function(schema){
                try
                {
                    if(schema )
                    {
                        Rsd.isEmpty( me.formFields)
                        {
                            me.formFields = schema.fields;
                        }
                        if(Rsd.isEmpty( me.gridColumns))
                        {
                            me.gridColumns = schema.columns;
                        }

                    }

                }catch (ex)
                {
                    Rsd.closeWaiting();
                    Rsd.error('加载模型结构失败','Rsd.Faith.pages.ListPage',ex);
                }

            });
        }

    },

    /*
    * */
    load:function load(args,callback) {

        var _type = this.modelType;

        if(!_type.startWith('Rsd.'))
        {
            _type = Rsd.app.assembelyName + '.' + _type;
        }
        var _args = args||{};
        var _where = _args.where ||this.toolBar.getWhere();
        if(this.where)
        {
            _where.push.apply(_where,this.where);
        }
        if(this.menu.where)
        {
            _where.push.apply(_where,this.menu.where);
        }
        _args.where = _where;
        _args.modelType=_type;
        _args.sort = _args.sort||this.sort||this.menu.sort||[];
        //console.log(_args);
        this.callParent(_args,function(){

        });
    },
    /*
    * */
   rowDblClick:function rowDblClick(row,evt)
    {
        this.editItem(row);
    },
    /*
    * */
    editItem:function editItem(record)
    {
        var me = this;
        //debugger;
        var _record=record;
        var _titleField = me.titleField || me.titleMember || me.menu.titleField || me.menu.titleMember;
        var _sub = (_titleField== null || _titleField.length ==0) ?"titleField未设置" :(_record ? (_record[_titleField]||'-') :'*');
        var _title = ( this.formTitle ||'无标题') +  '[' + _sub + ']';
        var _type = this.modelType;
        if(!_type.startWith('Rsd.'))
        {
            _type = Rsd.app.assembelyName + '.' + this.modelType;
        }
        Rsd.app.showModelView(_type,this.formFields,_record,_title,this.readOnly?['cancel']:['save','cancel'],this.readOnly,this);
    },
    /*
    *
    * */
    newItem:function newItem()
    {
        var me = this;
        //debugger;
        var _record={};
        var _titleField = me.titleField || me.titleMember || me.menu.titleField || me.menu.titleMember;
        var _sub = (_titleField== null || _titleField.length ==0) ?"titleField未设置" :(_record ? (_record[_titleField]||'-') :'*');
        var _title = ( this.formTitle ||'无标题') +  '[' + _sub + ']';
        var _type = this.modelType;
        if(!_type.startWith('Rsd.'))
        {
            _type = Rsd.app.assembelyName + '.' + this.modelType;
        }
        Rsd.app.showModelView(_type,this.formFields,_record,_title,['save','cancel'],false,this);
    },
    /*
     * */
    openItem:function openItem(record,btns,readOnly) {
        var me = this;
        //debugger;
        var _record=record;
        var _titleField = me.titleField || me.titleMember || me.menu.titleField || me.menu.titleMember;
        var _sub = (_titleField== null || _titleField.length ==0) ?"titleField未设置" :(_record ? (_record[_titleField]||'-') :'*');
        var _title = ( this.formTitle ||'无标题') +  '[' + _sub + ']';
        var _type = this.modelType;
        if(!_type.startWith('Rsd.'))
        {
            _type = Rsd.app.assembelyName + '.' + this.modelType;
        }
        Rsd.app.showModelView(_type,this.formFields,_record,_title,btns||['cancel'],readOnly==undefined?true:readOnly,this);
    },
    /*
     * */
    deleteItem: function (record) {
        var me = this;
        //debugger;
        var _titleField = me.titleField || me.menu.titleField;
        var _sub = (_titleField== null || _titleField.length ==0) ?"titleField未设置" :(record ? (record[_titleField]||'-') :'*');
        var _title = ( this.formTitle ||'无标题')  +  '[' + _sub + ']' + '-删除记录';
        var _type = this.modelType;
        if(!_type.startWith('Rsd.'))
        {
            _type = Rsd.app.assembelyName + '.' + this.modelType;
        }
        Rsd.app.showModelView(_type,this.formFields,record,_title,this.readOnly?[]:['delete','cancel'],this.readOnly,this);

    }

},function (type) {

    var _modelTypeGetter = function () {

        if (Rsd.isEmpty(this.__modelType)) {
            this.__modelType = this.menu.modelType;
        }
        return this.__modelType;
    };
    var _modelTypeSetter = function (value) {

        this.__modelType = value;

    };
    this.defineProperty(type,"modelType", _modelTypeGetter, _modelTypeSetter,false);



    var _searchColumnsGetter = function _searchColumnsGetter() {

        return this.toolBar.searchColumns;
    }

    var _searchColumnsSetter = function _searchColumnsSetter(value) {

        this.toolBar.searchColumns = value;
    }

    this.defineProperty(type,"searchColumns", _searchColumnsGetter, _searchColumnsSetter,true);


    var _newButtonsGetter = function _newButtonsGetter() {

        return this.toolBar.newButtons;
    }

    var _newButtonsSetter = function _newButtonsSetter(value) {

        this.toolBar.newButtons = value;
    }

    this.defineProperty(type,"newButtons", _newButtonsGetter, _newButtonsSetter,true);
});